package com.raos.example.redisclient;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

/**
 * redis 分布式锁实现
 *
 * @author raos
 * @emil 991207823@qq.com
 * @date 2021/7/22 23:01
 */
public class RedisLockMain {

    private static RedissonClient redissonClient;

    static {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://192.168.235.130:6380")
                .setPassword("redis@123");
        redissonClient = Redisson.create(config);
    }

    public static void main(String[] args) throws InterruptedException {
        RLock rLock = redissonClient.getLock("updateRepo");
        for (int i = 0; i < 10; i++) {
            if (rLock.tryLock()) {
                //返回true，表示获得锁成功
                System.out.println("获得锁成功");
            } else {
                System.out.println("获得锁失败");
            }
            Thread.sleep(2000);
            rLock.unlock();
        }
    }

}
